<?php


namespace app\sports\company\service;

use think\Db;
use think\Exception;
use app\common\HTTP;
<<<<<<< HEAD
=======
use app\common\OSS;
>>>>>>> 768f83341487a6cf8af57a06c9a1354114f7380a
use think\facade\Log;
use app\sports\company\model\CompanyModel;
use app\common\InstanceTrait;
class CompanyCaiji
{
    private $user = '';
    private  $secret ='';
//    接口名:百欧公司列表 -- 只跑一次
//    接口频次:2/s 无限制
    private $url = 'open.sportnanoapi.com/api/sports/odds/company/list';

    protected function __construct()
    {
        $this->user = Env::get('nami.user');
        $this->secret = Env::get('nami.secret');
    }

//    采集百欧公司

    public function Execs(){
        try{
            $companyressult = json_decode(HTTP::getInstance()->doGet($this->url."?user=$this->user&secret=$this->secret"), true);
    //         组装数据
             $data=[];
             foreach ($companyressult as $value){
                $data[]=[
                    'company_id' =>$value['id'],
                    'name_zh'    =>$value['name_zh'],
                    'name_en'    =>$value['name_en'],
                    'create_time'      =>date('Y-h-d H:i:s',time()),
                    'update_time'      =>date('Y-h-d H:i:s',time()),
                ];
             }
            $result=CompanyModel::getInstance()->InsertCompanyAll($data);
             if (!$result) throw new Exception('百欧公司数据入库失败');

            Log::log('百欧公司采集完成写入');
        }catch(Exception $e){
<<<<<<< HEAD
            $error = json_encode([
                'msg' => $e->getMessage(),
                'line' => $e->getLine(),
                'code' => $e->getCode(),
            ],JSON_UNESCAPED_UNICODE);
            Log::error('百欧公司采集错误:'.$error);
=======
            Log::error('百欧公司采集错误:'.$e->getMessage());
>>>>>>> 768f83341487a6cf8af57a06c9a1354114f7380a
            return [];
        }


    }

    /*
     * 百欧公司数据更新
     */
    public function UpdateExecs(){
         try{
            $companyressult = json_decode(HTTP::getInstance()->doGet($this->url."?user=$this->user&secret=$this->secret"), true);
            $arrayid=[];
            $adddata=[];
            foreach ($companyressult as $v){
                array_push($arrayid,$v['id']);
            }
//            先从数据查出所以的百欧公司的数据的id
            $modelid=CompanyModel::getInstance()->QueryCompanyId();
//            然后切割返回的所有百欧公司的id,存在的走更新,不存在的走添加
//             在这里的就是数据库没有的数据,需要添加的
             $newcompanyid = array_diff($arrayid,$modelid);
//             循环遍历里面的数据,进行拆分
             foreach ($companyressult as $value){
                if (in_array($value['id'],$modelid)){
//                    这边数据是需要添加到数据库的
                    $adddata[]=[
                    'company_id' =>$value['id'],
                    'name_zh'    =>$value['name_zh'],
                    'name_en'    =>$value['name_en'],
                    'create_time'      =>date('Y-h-d H:i:s',time()),
                    'update_time'      =>date('Y-h-d H:i:s',time()),
                 ];

                }else{
//                    更新数据
//                     更细的走更新,添加的走添加的逻辑
                    $update = [
                        'company_id' =>$value['id'],
                        'name_zh'    =>$value['name_zh'],
                        'name_en'    =>$value['name_en'],
                        'create_time'      =>date('Y-h-d H:i:s',time()),
                        'update_time'      =>date('Y-h-d H:i:s',time()),
                    ];
                    CompanyModel::getInstance()->UpdateComanyId($value['id'],$update);
                }
             }


             if (isset($adddata) && (!empty($adddata))){
                 CompanyModel::getInstance()->InsertCompanyAll($adddata);
             }
<<<<<<< HEAD
            Log::log('info','百欧公司更新采集完成写入');
        }catch(Exception $e){
             $error = json_encode([
                'msg' => $e->getMessage(),
                'line' => $e->getLine(),
                'code' => $e->getCode(),
            ],JSON_UNESCAPED_UNICODE);
            Log::error('百欧公司更新采集错误:'.$error);
=======
            Log::log('百欧公司更新采集完成写入');
        }catch(Exception $e){
            Log::error('百欧公司更新采集错误:'.$e->getMessage());
>>>>>>> 768f83341487a6cf8af57a06c9a1354114f7380a
            return [];
        }
    }


}